<template>
  <div class="categoryedit">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="上级分类" :required="true">
        <el-select v-model="form.name">
          <el-option
            v-for="item in topCategor"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" class="iptchange" :required="true">
        <el-input v-model="Categorinfo.name" width="200"></el-input>
      </el-form-item>

      <el-form-item label="分类图片">
        <el-upload
          class="avatar-uploader"
          action="http://kumanxuan1.f3322.net:8360/admin/upload/goodNewPic"
          name="good_pic"
          :on-success="handleAvatarSuccess"
          :headers="token"
          :show-file-list="false"
        >
          <img v-if="posterURL" :src="posterURL" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="显示">
        <el-switch v-model="Categorinfo.delivery"></el-switch>
      </el-form-item>
      <el-form-item label="排序" class="iptchange">
        <el-input v-model="Categorinfo.sort_order"></el-input>
      </el-form-item>
      <el-form-item label="分类描述" :required="true">
        <el-input
          type="textarea"
          v-model="Categorinfo.front_name"
          class="textvalue"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(Categorinfo)"
          >保存</el-button
        >
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getTopCategory,
  getCategoryDetail,
  addCategory,
  uploadGoodPic,
  getUserByToken,
} from "../../request/httpApi";
export default {
  data() {
    return {
      //表单的部分属性
      form: {
        name: "",
        region: "",
        type: [],
        resource: "",
        desc: "",
      },
      //分类信息
      Categorinfo: {},
      //请求顶级分类数据
      topCategor: [],
      //Category.vue
      posterURL: "",
      //token
      token: {},
    };
  },
  created() {
    //获取用户信息
    this.token = {
      "X-Nideshop-Token": localStorage.getItem("x-auth-token"),
    };

    //发送顶级分类请求
    getTopCategory().then((res) => {
      // console.log(res);
      this.topCategor = res.data;
      console.log(res.data[0]);
      this.form = res.data.name;
      res.data.map((item) => {
        // console.log(item.name);
        this.form = item;
      });
    });

    // //发送商品信息请求
    getCategoryDetail({ id: this.$route.params.cid }).then((res) => {
      console.log(res);
      if (res.errno == 0) {
        this.Categorinfo = res.data;
        this.posterURL = res.data.wap_banner_url;
      }
    });
  },
  methods: {
    //图片的方法
    handleAvatarSuccess(file, fileList) {
      this.posterURL = file.data.fileUrl;
   
      // console.log(this.posterURL);
      // console.log(fileList);
    },
    onSubmit(val) {
  
      //当我点击保存的时候, 这个东西能发去后台
   
      addCategory({
        banner_url: this.posterURL,
        front_desc: val.front_desc,
        front_name: val.front_name,
        icon_url: this.posterURL,
        id: val.id,
        img_url: this.posterURL,
        is_new: val.is_new,
        is_show: val.is_show,
        keywords: val.keywords,
        level: val.level,
        name: val.name,
        parent_id: val.parent_id,
        show_index: val.show_index,
        sort_order: val.sort_order,
        type: val.type,
        wap_banner_url: this.posterURL,
      }).then((res) => {
        console.log(res);
      });
      this.$router.push("/category");
    },
  },
};
</script>

<style lang="less" scoped>
.categoryedit {
  background-color: #fff;
  padding-top: 10px;

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .iptchange {
    width: 296px;
  }
  .textvalue {
    width: 215px;
  }
  /deep/ .el-textarea__inner {
    padding: 38px 0px;
  }
}
</style>